import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class jdbcdemo3_Connection {
    public static void main(String[] args) throws Exception{
        Class.forName("com.mysql.jdbc.Driver");
        /*本机MySQL并且端口3306可以简化书写
        String url = "jdbc:mysql://cc";
         */
                String url = "jdbc:mysql://192.168.2.131:3306/cc";
                String username = "root";
                String password = "abc123";
        Connection conn = DriverManager.getConnection(url,username,password);
        //定义SQL
        String sql1 = "update account set money = 2000 where id = 1";
        String sql2 = "update account set money = 3000 where id = 2";
        //获取sql的对象statement
        Statement stmt = conn.createStatement();
        //开启事务
        conn.setAutoCommit(false);
        try {
            //执行sql
            int count1 = stmt.executeUpdate(sql1);
            //处理结果
            System.out.println(count1);
            //执行sql
            int count2 = stmt.executeUpdate(sql2);
            //处理结果
            System.out.println(count2);
            //提交事务
            conn.commit();
        } catch (Exception e) {
            //回滚事务
            conn.rollback();
            throw new RuntimeException(e);
        }
        //释放资源
        stmt.close();
        conn.close();
        System.out.println("==============");

    }
}

